<?PHP

class OP_PagesWidget extends OP_WidgetTemplate{
	function render(){
		
		$title = empty( $this->values['title'] ) ? __( 'Pages' ) : apply_filters('widget_title', $this->values['title']);
		$sortby = empty( $this->values['sortby'] ) ? 'menu_order' : $this->values['sortby'];
		$exclude = empty( $this->values['exclude'] ) ? '' : $this->values['exclude'];

		if ( $sortby == 'menu_order' ) {
			$sortby = 'menu_order, post_title';
		}

		$out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );

		if ( !empty( $out ) ) {
			echo $this->values['before_title'] . $title . $this->values['after_title'],
				'<ul>',
				$out,
				'</ul>';
		}
	}
	
	function do_post($post){
		$title = strip_tags(stripslashes($post['title']));

		$sortby = stripslashes( $post['sortby'] );

		if ( !in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) {
			$sortby = 'menu_order';
		}

		$exclude = strip_tags( stripslashes( $post['exclude'] ) );

		return compact( 'title','sortby','exclude');
	}
	
	function options(){
		$number = $this->values['number'];

		echo '<p><label for="op_pages-title-'.$number.'">'._e('Title:').'<input class="widefat" id="op_pages-title-'.$number.'" name="'.$this->widget->class.'['.$number.'][title]" type="text" value="'.$this->values['title'].'" /></label></p>',
			'<p>',
			'<label for="op_pages-sortby-'.$number.'">'._e( 'Sort by:' ),
				'<select name="'.$this->widget->class.'['.$number.'][sortby]" id="op_pages-sortby-'.$number.'" class="widefat">',
					'<option value="post_title" '.( $this->values['sortby'] == 'post_title' ? 'SELECTED' : '').'>Page title</option>',
					'<option value="menu_order" '.($this->values['sortby'] == 'menu_order' ? 'SELECTED' : '').'>Page order</option>',
					'<option value="ID" '.($this->values['sortby'] == 'ID' ? 'SELECTED' : '').'>Page ID</option>',
				'</select>',
			'</label>',
		'</p>',
		'<p>',
			'<label for="'.$this->widget->class.'-exclude-'.$number.'">'._e( 'Exclude:' ).' <input type="text" value="'.$this->values['exclude'].'" name="'.$this->widget->class.'['.$number.'][exclude]" id="'.$this->widget->class.'-exclude-'.$number.'" class="widefat" /></label>',
			'<br />'.
			'<small>'._e( 'Page IDs, separated by commas.' ).'</small>',
		'</p>';
	}
}

$widget = new OP_Widget('pages');
$widget->setMeta('OP Pages',"Your site's pages");
$widget->setDefaults(array('title'=>''));

$opw_pages = new OP_PagesWidget($widget);
add_action('widgets_init', array($opw_pages, 'init'));

?>